﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Estimate
{
    public partial class formBudgetPayment : FormMDIChildTemplate
    {
        public formBudgetPayment()
        {
            InitializeComponent();



        }


        void SelectPayment()
        {
            paymentBindingSource.DataSource = checkBoxObject.Checked ? context.Payments.Where(x => x.Object == comboBoxObject.SelectedItem).Select(x => x) : context.Payments.Where(x => x.Firm == comboBoxFirm.SelectedItem).Select(x => x);
        }

        private void formBudgetPayment_Load(object sender, EventArgs e)
        {
            root.TranslateGrid(dataGridView1);
            firmBindingSource .DataSource = context.Firms.OrderBy(x => x.FirmName).Where(x => x.FirmTypeID == 3);
           // comboBoxObject.DataSource = context.Objects.OrderBy(x => x.ObjectShortName);
            SelectPayment();
        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            Payment payment = (Payment)paymentBindingSource.AddNew();
            payment.Firm = (Firm)comboBoxFirm.SelectedItem;
            payment.Object = checkBoxObject.Checked ? (Object)comboBoxObject.SelectedItem : null;
            payment.PaymentDate = Convert.ToDateTime(DateTime.Now.ToShortDateString());
            payment.PaymentSum = 0;
            context.Payments.InsertOnSubmit(payment);
            root.Submit();
            SelectPayment();

        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {

        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            context.Payments.DeleteOnSubmit((Payment)paymentBindingSource.Current);
            root.Submit();
            SelectPayment();


        }

        private void comboBoxFirm_SelectedIndexChanged(object sender, EventArgs e)
        {


        }

        private void comboBoxObject_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectPayment();
        }

        private void checkBoxObject_CheckedChanged(object sender, EventArgs e)
        {
            comboBoxObject.Enabled = checkBoxObject.Checked;
        }

        private void firmBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            Firm firm = (Firm)firmBindingSource.Current;
            if (firm != null) comboBoxObject.DataSource = context.Objects.
                OrderBy(x => x.ObjectShortName).Where(x => x.Firm == firm && x.IsReal);
            SelectPayment();
        }
    }
}